package cn.shoukaiseki.sjf4j.fastjson;

import cn.shoukaiseki.sjf4j.JsonException;
import cn.shoukaiseki.sjf4j.node.BaseTreeNode;
import cn.shoukaiseki.sjf4j.node.NumberType;
import cn.shoukaiseki.sjf4j.node.TreeNode;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import java.math.BigDecimal;

public abstract class FastjsonTreeNode<T> extends BaseTreeNode {

   protected T value;


    public FastjsonTreeNode(T value){
        this.value = value;
//        initType(this.value);
    }

//    public void initType(Object val){
//        if (val==null) {
//           this.jsonNodeType=JsonNodeType.NULL;
//            this.numberType=null;
//           return;
//        }
//        if(val instanceof JSONObject){
//
//            return;
//        }
//        throw new JsonException("转换不完善,fastjsonNode类名 "+val.getClass());
//
//    }



    public static TreeNode convert(Object jsonNode){
        try{
            if (jsonNode==null) {
                return new FastjsonNullNode();
            }
            if (jsonNode instanceof JSONObject) {
                return new FastjsonObjectNode((JSONObject) jsonNode);
            }
            if(jsonNode instanceof JSONArray){
                return new FastjsonArrayNode((JSONArray)jsonNode);
            }
            if(jsonNode instanceof String){
                return new FastjsonTextNode((String) jsonNode);
            }
            if(jsonNode instanceof Long){
                return new FastjsonLongNode((Long) jsonNode);
            }
            if(jsonNode instanceof Double){
                return new FastjsonDoubleNode((Double) jsonNode);
            }
            if(jsonNode instanceof BigDecimal){
                return new FastjsonBigDecimalNode((BigDecimal) jsonNode);
            }
            if(jsonNode instanceof Integer){
                return new FastjsonIntNode((Integer) jsonNode);
            }
            if(jsonNode instanceof Boolean){
                return new FastjsonBooleanNode((Boolean) jsonNode);
            }
        } catch (Throwable t) {
            throw new JsonException(t);
        }
        throw new JsonException("转换不完善,jacksonNode类名 "+jsonNode.getClass());
    }

    @Override
    public NumberType numberType() {
        return null;
    }

    @Override
    public int size() {
        return 0;
    }

    @Override
    public boolean isShort() {
        return false;
    }

    @Override
    public boolean canConvertToInt() {
        return false;
    }

    @Override
    public int asInt() {
        return 0;
    }

    @Override
    public long asLong() {
        return 0;
    }


    @Override
    public String toString() {
        return value.toString();
    }

    @Override
    public boolean asBoolean() {
        return false;
    }

    @Override
    public double asDouble() {
        return 0;
    }

    @Override
    public BigDecimal asDecimalVal() {
        return null;
    }
}
